package com.ayuku.commons;

import java.math.BigDecimal;

import org.eclipse.nebula.widgets.formattedtext.FormattedText;
import org.eclipse.nebula.widgets.formattedtext.ITextFormatter;
import org.eclipse.nebula.widgets.formattedtext.NumberFormatter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.wb.swt.SWTResourceManager;

public class AyuBigDecimal extends org.eclipse.swt.widgets.Composite {
	private FormattedText orgComponent;

	public FormattedText getOrgComponent() {
		return orgComponent;
	}

	public void setOrgComponent(FormattedText orgComponent) {
		this.orgComponent = orgComponent;
	}

	public AyuBigDecimal(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		initGUI();
	}

	public void setTextLimit(int n) {
		orgComponent.getControl().setTextLimit(n);
	}

	public void addKeyListener(KeyListener arg0) {
		orgComponent.getControl().addKeyListener(arg0);
	}
	
	public void addModifyListener(ModifyListener arg0) {
		orgComponent.getControl().addModifyListener(arg0);
	}

	private void initGUI() {
		try {
			FillLayout thisLayout = new FillLayout(
					org.eclipse.swt.SWT.HORIZONTAL);
			this.setLayout(thisLayout);
			this.setSize(264, 25);
			orgComponent = new FormattedText(this, SWT.NONE | SWT.RIGHT);
			orgComponent.setFormatter(new NumberFormatter(
					"-###,###,###,##0.###"));

			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setFormatter(ITextFormatter formater) {
		orgComponent.setFormatter(formater);
	}

	public BigDecimal getValue() {
		BigDecimal val = new BigDecimal("0.00");

		if (orgComponent.getValue() != null) {
			val = new BigDecimal(orgComponent.getValue().toString());
		}
		return val;
	}

	public void setEnabled(boolean bln) {
		orgComponent.getControl().setEditable(bln);
		if (bln) {
			orgComponent.getControl().setBackground(
					SWTResourceManager.getColor(255, 255, 255));
		} else {
			orgComponent.getControl().setBackground(
					SWTResourceManager.getColor(236, 233, 216));
		}
	}

	public void setFont(Font fnt) {
		orgComponent.getControl().setFont(fnt);
	}

	public void setForeground(Color fnt) {
		orgComponent.getControl().setForeground(fnt);
	}

	public void setBackground(Color fnt) {
		orgComponent.getControl().setBackground(fnt);
	}

	public void setValue(BigDecimal obj) {
		if (obj == null) {
			obj = new BigDecimal("0");
		}
		orgComponent.setValue(obj);
	}

	public void setStringFormatter(String args) {
		orgComponent.setFormatter(new NumberFormatter(args));
	}
}
